/*
 * This is the JS for initialize CareTrack mobile application.
 */

//We will follow this namespace crossing our client side code.
function registerNamespace(ns) {
	var nsParts = ns.split('.');
	var root = window;
	for (var i = 0; i < nsParts.length; i++) {
		if (typeof root[nsParts[i]] == 'undefined')
			root[nsParts[i]] = {};
		root = root[nsParts[i]];
	}
}

registerNamespace('Volvo.CareTrack');



Volvo.CareTrack.GlobalClass = function() {
	this.settings = { 
//			jsonCallHost : 'http://m.caretrack.com/caretrack/jsonapi/services'
			jsonCallHost : 'http://10.0.2.2/caretrack/jsonapi/services'
//			jsonCallHost : 'http://localhost/caretrack/jsonapi/services'
	};
};

proto = Volvo.CareTrack.GlobalClass.prototype;

proto.init = function() {
	console.log('initializing catetrack mobile app');
	$(document).bind("mobileinit", function() {
		console.log('mobileinit start ...');
		Volvo.CareTrack.PageRenderer.registPages();
		console.log('mobileinit completed!');
	});
	// Ajax defaults
	$.ajaxSetup({dataType: 'jsonp', timeout: 3000});
}





//Common utility functions
Volvo.CareTrack.UtilitiesClass = function() {};

proto = Volvo.CareTrack.UtilitiesClass.prototype;

proto.getRequestParameter = function ( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

proto.getFromStorage = function(name) {
//	return localStorage[name];
	return $.cookie(name);
}

proto.setToStorage = function(name, value) {
//	localStorage[name] = value;
	$.cookie(name,value);
}

proto.removeFromStorage = function(name) {
//	var storage = window.localStorage;
//	 storage.removeItem(name);
	$.cookie(name,null);
}

proto.checkNetwork = function(runParams) {
	var hasConnection = false;
	function reachableCallback(reachability) {
		var networkState = reachability.code || reachability;
		var states = {};
		states[NetworkStatus.NOT_REACHABLE]                      = 'No network connection';
		states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
		states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK]         = 'WiFi connection';

		console.log('Connection type: ' + states[networkState]);
		
		//(states[networkState] == NetworkStatus.NOT_REACHABLE) ? runParams.error : runParams.success;
		hasConnection = (states[networkState] == NetworkStatus.NOT_REACHABLE);
	}	
	navigator.network.isReachable('phonegap.com', reachableCallback);
	return hasConnection;
}

proto.checkBackend = function(runParams) {
	//TODO
	
}

proto.checkGoogleMap = function(runParams) {
	//TODO
	return true;
}

Volvo.CareTrack.Utilities = new Volvo.CareTrack.UtilitiesClass();

//replaces the class definition with a newly created singleton
Volvo.CareTrack.Global = new Volvo.CareTrack.GlobalClass();


//Launch CareTrack mobile application
Volvo.CareTrack.Global.init();
